package com.lu.netty.io.client

import com.lu.netty.io.core.NettyConfigure
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.handler.codec.string.StringDecoder
import io.netty.handler.codec.string.StringEncoder
import io.netty.handler.timeout.IdleStateHandler
import java.util.concurrent.TimeUnit

class ClientChannelHandler(
    private val callback: IClientAdapterCallback
) : ChannelInitializer<Channel>() {

    companion object {
        private const val TAG = "ClientChannelHandler"
    }

    override fun initChannel(channel: Channel) {
        val pipeline = channel.pipeline()
        pipeline.addLast(
            IdleStateHandler(
                NettyConfigure.readIdleTime,
                NettyConfigure.writeIdleTime,
                NettyConfigure.allIdleTime,
                TimeUnit.MILLISECONDS
            ),
//            DelimiterBasedFrameDecoder(1024, Unpooled.copiedBuffer("###".toByteArray())),
            StringDecoder(),
            StringEncoder(),
            ClientCHAdapter("client", callback),
        )
    }
}